const { createOrder, findAllOrders, updateOrder } = require('../service/order.service')
class OrderController {
  async create(ctx) {
    const user_id = ctx.state.user.id
    const { address_id, goods_info, total } = ctx.request.body

    const order_number = 'learn' + Date.now()

    const res = await createOrder({ user_id, order_number, address_id, goods_info, total })
    ctx.body = {
      code: "200",
      message: "创建订单成功",
      result: res
    }
  }
  async findAll(ctx) {
    const { pageNum = 1, pageSize = 10, status = 0 } = ctx.request.query
    const res = await findAllOrders(pageNum, pageSize, status)
    ctx.body = {
      code: "200",
      message: "查询订单成功",
      result: res
    }
  }
  async update(ctx) {
    const id = ctx.request.params.id
    const { status } = ctx.request.body
    const res = await updateOrder(id, status)
    ctx.body = {
      code: "200",
      message: "更新订单状态成功",
      result: res
    }
  }
}

module.exports = new OrderController()